package com.subfg.payment.server;

import com.subfg.common.constant.PayMethodEnum;
import com.subfg.common.entity.call.request.refund.ApplyRefundModel;
import com.subfg.common.entity.call.result.RefundHandlerModel;
import com.subfg.common.entity.config.pay.ConfigPayAliModel;
import com.subfg.common.util.common.TimeUtil;
import com.subfg.payment.dao.config.ConfigAliDao;
import com.subfg.payment.dao.config.ConfigStripeDao;
import com.subfg.payment.util.ali.AliPayServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

@Service
public class PaymentServer {
    private Logger log = LoggerFactory.getLogger(PaymentServer.class);

    @Autowired
    private ConfigStripeDao configStripeDao;
    @Autowired
    private ConfigAliDao configPayAliDao;
    @Autowired
    private AliPayServer aliPayServer;


    public ConfigPayAliModel getPaymentAliInfo() {
        ConfigPayAliModel configPayAliModel = configPayAliDao.getConfigAliPayInfo();
        if (configPayAliModel == null) {
            log.error("严重错误, 支付宝获取支付配置数据为空!!! 这可能导致AliPay 支付不能使用!!!!");
            return null;
        }
        try {
            configPayAliModel = ConfigPayAliModel.decodeAlipayConfigInfo(configPayAliModel);
            return configPayAliModel;
        } catch (Exception e) {
            log.error("严重错误, 支付宝获取支付配置数据为空!!! 这可能导致AliPay 支付不能使用!!!!");
            return null;
        }
    }


    /** 退款信息处理 **/
    public RefundHandlerModel refundHandler(List<ApplyRefundModel.RefundOrderModel> refundOrderModelList) {
        if (!refundOrderModelList.isEmpty()) {;
            Iterator<ApplyRefundModel.RefundOrderModel> iterator = refundOrderModelList.listIterator();
            List<ApplyRefundModel.RefundOrderModel> successList = new LinkedList<>();
            List<ApplyRefundModel.RefundOrderModel> failList = new LinkedList<>();
            while (iterator.hasNext()) {
                ApplyRefundModel.RefundOrderModel refundInfo = iterator.next();
                try {
                    PayMethodEnum payMethod = PayMethodEnum.getSysPatMethod(refundInfo.getChannel());
                    switch (payMethod) {
                        case PAY_ALI:
                            refundInfo = aliPayServer.refundAliPay(refundInfo);
                            break;
                        case PAY_WECHAT:
                            break;
                        case PAY_STRIPE:
                            break;
                        default:
                            log.warn("未找到本系统合适的支付渠道==>>>{}", payMethod.getPayType());
                    }
                    if (refundInfo.getRefundStatus() == 1) {
                        successList.add(refundInfo);
                    } else {
                        failList.add(refundInfo);
                    }
                } catch (Exception e) {
                    log.error("执行退款失败!! 失败信息如下" + e);
                    failList.add(refundInfo);
                }
            }
            return new RefundHandlerModel().builder()
                    .refundCount(refundOrderModelList.size())
                    .handlerTime(TimeUtil.getCurrentTime())
                    .successRefundOrderList(successList)
                    .failRefundOrderList(failList)
                    .build();
        }
        return null;
    }
}
